﻿using System.Collections.Generic;
using System.Linq;

namespace Common.EF.Structs
{
    public static class EntityTypeManager
    {
        private static List<EntityType> entityConfigs = new List<EntityType>();

        public static List<EntityType> EntityTypes
        {
            get { return entityConfigs; }
        }

        public static EntityType GetEntityType(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return null;
            }

            if (name.StartsWith("m_"))
            {
                name = "t_" + name.Substring(2);
            }

            return EntityTypes.FirstOrDefault(t => string.Compare(t.Name, name, true) == 0);
        }

        public static void AddEntityType(EntityType entityType)
        {
            if (string.IsNullOrEmpty(entityType.Name))
            {
                return;
            }

            var old = EntityTypes.FirstOrDefault(t => string.Compare(t.Name, entityType.Name, true) == 0);
            if (old != null)
            {
                EntityTypes.Remove(old);
            }

            EntityTypes.Add(entityType);
        }
    }
}
